package strings;

import static net.mindview.util.Print.print;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StartEndMY {
	public static String input = "As long as there is injustice, whenever a\n"
			+ "Targathian baby cries out, wherever a distress\n"
			+ "signal sounds among the stars ... We'll be there.\n"
			+ "This fine ship, and this fine crew ...\n"
			+ "Never give up! Never surrender!";

	private static class Display {
		private boolean regexPrinted = false;
		private String regex;

		Display(String regex) {
			this.regex = regex;
		}

		void display(String message) {
			if (!regexPrinted) {
				print(regex);
				regexPrinted = true;
			}
			print(message);
		}
	}

	static void examine(String s, String regex) {
		Display display = new Display(regex);
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(s);
		
		//{"\\w*ere\\w*", "\\w*ever", "T\\w+", "Never.*?!"}
		while (matcher.find()) {
			display.display("1 -> find() '" + matcher.group() + "' start = "
					+ matcher.start() + " end = " + matcher.end());
		}
		
		if (matcher.lookingAt()) // No reset() necessary
		{
			display.display("2 -> lookingAt() start = " + matcher.start()
					+ " end = " + matcher.end());
		}
		
		if (matcher.matches()) // No reset() necessary
		{
			display.display("3 -> matches() start = " + matcher.start() + " end = "
					+ matcher.end());
		}
	}

	public static void main(String[] args) {
		for (String in : input.split("\n")) {
			print("=========================");
			print("in : " + in);

			List<String> list = new ArrayList<String>();
			list.add("\\w*ere\\w*");
			list.add("\\w*ever");
			list.add("T\\w+");
			list.add("Never.*?!");

			for (String regex : list.toArray(new String[] {}))
				examine(in, regex);
		}
	}
}
